package jmu.cultural_tourism.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jmu.cultural_tourism.entity.PostReply;
import jmu.cultural_tourism.service.PostReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import java.util.List;

/**
 * (PostReply)表控制层
 *
 * @author makejava
 * @since 2025-07-17 15:31:34
 */
@RestController
@Tag(name="帖子评论接口")
@RequestMapping("postReply")
public class PostReplyController {
    /*服务对象*/
    @Autowired
    private PostReplyService postReplyService;

    /*分页查询*/
    @GetMapping
    public ResponseEntity<Page<PostReply>> queryByPage(PostReply postReply, PageRequest pageRequest) {
        return ResponseEntity.ok(this.postReplyService.queryByPage(postReply, pageRequest));
    }

    /*通过主键查询单条数据*/
    @GetMapping("{id}")
    public ResponseEntity<PostReply> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.postReplyService.queryById(id));
    }

    /*通过帖子ID查询评论数据*/
    @GetMapping("findByPostId")
    @Operation(summary = "通过帖子ID查询对应评论")
    public ResponseEntity<List<PostReply>> queryByPostId(
            @RequestParam Integer postId
    ) {
        return ResponseEntity.ok(this.postReplyService.queryByPostId(postId));
    }

    /*新增数据*/
    @PostMapping("add")
    @Operation(summary = "新增帖子评论")
    public ResponseEntity<PostReply> add(@RequestBody PostReply postReply) {
        postReply.setCreateTime(new java.util.Date());
        return ResponseEntity.ok(this.postReplyService.insert(postReply));
    }

    /*编辑数据*/
    @PutMapping
    public ResponseEntity<PostReply> edit(PostReply postReply) {
        return ResponseEntity.ok(this.postReplyService.update(postReply));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.postReplyService.deleteById(id));
    }

}

